package com.itheima.controller.user;


import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderTOD;
import com.itheima.pojo.OrderVO2;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/order")
public class OrderController {


    @Autowired
    OrderService orderService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("/findById")
    public Result findById(@RequestParam String id){

        Integer id1 = Integer.parseInt(id);
        OrderVO2 orderVO2 = orderService.findById(id1);
        return new Result(true,"查询成功",orderVO2);

    }

    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderTOD orderTOD){


//        判断验证码是否正确
        String code = (String)redisTemplate.opsForValue().get(orderTOD.getTelephone());

        String code2= orderTOD.getValidateCode();
        if (!code.contains(code2) ){
            return new Result(false,"验证码错误");
        }

         Integer id = orderService.add(orderTOD);
//        删除验证码
        redisTemplate.delete(orderTOD.getTelephone());
        return new Result(true,"预约成功",id);

    }



}
